package com.oo.app.utils;

import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Glide 图片加载辅助类
 * 适配圆形图片加载情况
 */

public class ImageLoader {
    private ImageLoader() {
    }

    public static void loadImage(RequestManager loader, ImageView view, String url) {
        loadImage(loader, view, url, 0);
    }

    public static void loadImage(RequestManager loader, ImageView view, String url, int placeholder) {
        loadImage(loader, view, url, placeholder, placeholder);
    }

    public static void loadImage(RequestManager loader, ImageView view, String url, int placeholder, int error) {
        boolean isCenterCrop = false;
        if (view instanceof CircleImageView)
            isCenterCrop = true;
        loadImage(loader, view, url, placeholder, error, isCenterCrop);
    }

    public static void loadImage(RequestManager loader, ImageView view, String url, int placeholder, int error, boolean isCenterCrop) {
        if (TextUtils.isEmpty(url)) {
            view.setImageResource(placeholder);
        } else {
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(placeholder)
                    .error(placeholder);
            loader.load(url).apply(options).into(view);
//            if (view instanceof CircleImageView) {
//                BitmapRequestBuilder builder = loader.load(url).asBitmap()
//                        .diskCacheStrategy(DiskCacheStrategy.ALL)
//                        .placeholder(placeholder)
//                        .error(error);
//                if (isCenterCrop)
//                    builder.centerCrop();
//
//                builder.into(
//                        new BitmapImageViewTarget(view) {
//                            @Override
//                            protected void setResource(Bitmap resource) {
//                                RoundedBitmapDrawable circularBitmapDrawable =
//                                        RoundedBitmapDrawableFactory.create(view.getResources(), resource);
//                                circularBitmapDrawable.setCircular(true);
//                                view.setImageDrawable(circularBitmapDrawable);
//                            }
//                        });
//            } else {
//                DrawableRequestBuilder builder = loader.load(url).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(placeholder).error(error);
//                if (isCenterCrop)
//                    builder.centerCrop();
//                builder.into(view);
//            }
        }
    }
}
